/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package VendaDeCarros;

import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import javax.jdo.PersistenceManager;

/**
 *
 * @author Dalhanges
 */
public class VeiculoBean {

    private static final PersistenceManager pm = PMF.get().getPersistenceManager();
    private List<Veiculos> veiculos;
    private String email;
    private String senha;
    private String placa;
    private String marca;
    private ArrayList<SelectItem> marcaList = null;
    private String modelo;
    private Integer anoFabricacao;
    private Integer anoModelo;
    private String cor;
    private String combustivel;
    private ArrayList<SelectItem> combustivelList = null;
    private Integer km;
    private Integer portas;
    private ArrayList<SelectItem> portasList = null;
    private Float valorVista;
    private String caracteristicas;

    public VeiculoBean() {
    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

    public String getSenha() {
        return senha;
    }
    public void setSenha(String senha) {
        this.senha = senha;
    }
    
    public String getPlaca() {
        return placa;
    }
    public void setPlaca(String placa) {
        this.placa = placa;
    }

    public String getMarca() {
        return marca;
    }
    public void setMarca(String marca) {
        this.marca = marca;
    }

    public ArrayList<SelectItem> getMarcaList() {
        this.marcaList = new ArrayList<SelectItem>();
        this.marcaList.add(new SelectItem("1", "Chevrolet"));
        this.marcaList.add(new SelectItem("2", "Fiat"));
        this.marcaList.add(new SelectItem("3", "Ford"));
        this.marcaList.add(new SelectItem("4", "Hyundai"));
        this.marcaList.add(new SelectItem("5", "Nissan"));
        this.marcaList.add(new SelectItem("6", "Renault"));
        this.marcaList.add(new SelectItem("7", "Volkswagen"));

        return this.marcaList;
    }
    public void setMarcaList(ArrayList<SelectItem> marcaList) {
        this.marcaList = marcaList;
    }

    public String getModelo() {
        return modelo;
    }
    public void setModelo(String modelo) {
        this.modelo = modelo;
    }

    public Integer getAnoFabricacao() {
        return anoFabricacao;
    }
    public void setAnoFabricacao(Integer anoFabricacao) {
        this.anoFabricacao = anoFabricacao;
    }

    public Integer getAnoModelo() {
        return anoModelo;
    }
    public void setAnoModelo(Integer anoModelo) {
        this.anoModelo = anoModelo;
    }

    public String getCor() {
        return cor;
    }
    public void setCor(String cor) {
        this.cor = cor;
    }

    public String getCombustivel() {
        return combustivel;
    }
    public void setCombustivel(String combustivel) {
        this.combustivel = combustivel;
    }

    public ArrayList<SelectItem> getCombustivelList() {
        this.combustivelList = new ArrayList<SelectItem>();
        this.combustivelList.add(new SelectItem("1", "Diesel"));
        this.combustivelList.add(new SelectItem("2", "Etanol"));
        this.combustivelList.add(new SelectItem("3", "Flex"));
        this.combustivelList.add(new SelectItem("4", "Gasolina"));
        this.combustivelList.add(new SelectItem("5", "GNV"));
        return this.combustivelList;
    }
    public void setCombustivelList(ArrayList<SelectItem> combustivelList) {
        this.combustivelList = combustivelList;
    }

    public Integer getKm() {
        return km;
    }
    public void setKm(Integer km) {
        this.km = km;
    }

    public Integer getPortas() {
        return portas;
    }
    public void setPortas(Integer portas) {
        this.portas = portas;
    }

    public ArrayList<SelectItem> getPortasList() {
        this.portasList = new ArrayList<SelectItem>();
        this.portasList.add(new SelectItem("2", "2"));
        this.portasList.add(new SelectItem("4", "4"));

        return this.portasList;
    }
    public void setPortasList(ArrayList<SelectItem> portasList) {
        this.portasList = portasList;
    }

    public Float getValorVista() {
        return valorVista;
    }
    public void setValorVista(Float valorVista) {
        this.valorVista = valorVista;
    }
    public String getCaracteristicas() {
        return caracteristicas;
    }
    public void setCaracteristicas(String caracteristicas) {
        this.caracteristicas = caracteristicas;
    }

    public String cadastrar() {
        Veiculos veiculos = 
                new Veiculos(this.email, this.senha, this.placa, 
                this.marca, this.modelo, this.anoFabricacao, 
                this.anoModelo, this.cor, this.combustivel, 
                this.km, this.portas, this.valorVista, 
                this.caracteristicas);
        
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
                pm.makePersistent(veiculos);
        } finally {
            pm.close();
        }

        return "inicio";
    }
    public String buscar(){
    PersistenceManager pm = PMF.get().getPersistenceManager();
    String query = "select from " + Veiculos.class.getName()+
                   " where modelo == '" + marca + "'";
    List<Veiculos> veiculos = (List<Veiculos>) pm.newQuery(query).execute();
    return null;
    }

    public void setVeiculos(List<Veiculos> veiculos) {
        this.veiculos = veiculos;
    }
    public List<Veiculos> getVeiculos(){
        String query =
                "select from "+Veiculos.class.getName()+
                " where marca == '"+marca+"'";

        setVeiculos((List<Veiculos>) pm.newQuery(query).execute());
        
        return this.veiculos;
    }
}
